package weaver.formmode.customjavacode.modeexpand.unionpay.market;

import cn.hutool.core.util.StrUtil;
import com.customization.unionpay.market.service.ClearFundDetailsButtonService;
import com.customization.unionpay.market.service.impl.ClearFundDetailsButtonServiceImpl;
import com.customization.unionpay.market.util.FormDataUtils;
import com.customization.unionpay.market.util.PropUtils;
import com.engine.common.util.ServiceUtil;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.soa.workflow.request.RequestInfo;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author sjm
 * @description 清算资金明细相关action
 */
public class ClearFundDetailsBatchImport extends AbstractModeExpandJavaCodeNew {

    private static final Logger log = LoggerFactory.getLogger(ClearFundDetailsBatchImport.class);

    private ClearFundDetailsButtonService getService(User user) {
        return ServiceUtil.getService(ClearFundDetailsButtonServiceImpl.class, user);
    }

    /**
     * 执行模块扩展动作
     *
     * @param param param包含(但不限于)以下数据
     *              user 当前用户
     *              importtype 导入方式(仅在批量导入的接口动作会传输) 1 追加，2覆盖,3更新，获取方式(int)param.get("importtype")
     *              导入链接中拼接的特殊参数(仅在批量导入的接口动作会传输)，比如a=1，可通过param.get("a")获取参数值
     *              页面链接拼接的参数，比如b=2,可以通过param.get("b")来获取参数
     * @return
     */
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        log.info("inClearFundDetailsBatchImport");
        Map<String, String> result = new HashMap<>();
        try {
            User user = (User) param.get("user");
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());

                String filename = Util.null2String(param.get("filename"));
                log.info("filename == >" +filename);
                if (billid > 0 && modeid > 0) {
                    //------请在下面编写业务逻辑代码------
                    // 【业务类型+资金来源+清算日期+数据来源】为唯一条件
                    /*
                       业务类型        ywlx 资金来源        zjly 清算日期        qsrq 数据来源        sjly 清算金额        qsje
                       业务类型        ywlx 资金来源        zjly 清算日期        qsrq 数据来源        sjly 清算金额合计     qsjehj
                    */
                    // 清算资金汇总台账表单id
                    int formId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "clearing_fund_summary_ledger_form_id"));
                    log.info("formId == >" +formId);
                    // 清算资金汇总台账模块id
                    int formmodeId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "clearing_fund_summary_ledger_formmode_id"));
                    log.info("formmodeId == >" +formmodeId);

                    Map<String, Object> allFormData = FormDataUtils.getMainFormData(requestInfo);
                    if(allFormData != null) {
                        // 字段头
                        String propValue = PropUtils.BB_LOG.getPropValue("marketSubject", "clearing_fund_summary_ledger_import_field");
                        log.info("propValue == >" + propValue);
                        String oldValueCode = PropUtils.BB_LOG.getPropValue("marketSubject", "oldValueCode");
                        log.info("oldValueCode == >" + oldValueCode);
                        String newValueCode = PropUtils.BB_LOG.getPropValue("marketSubject", "newValueCode");
                        log.info("newValueCode == >" + newValueCode);
                        String zs = new String(propValue.getBytes(oldValueCode), newValueCode);
                        log.info("zs:" + zs);
                        String propValueChange = URLDecoder.decode(zs, "UTF-8");
                        log.info("clearing_fund_summary_ledger_import_field:" + propValueChange);
                        String[] values = propValueChange.split(",");
                        boolean going = true;
                        for (String value : values) {
                            String key = value.split("-")[1];
                            Object o = allFormData.get(key);
                            if ("".equals(o)) {
                                going = false;
                                break;
                            }
                        }
                        if (going) {
                            String ywlx = null;
                            String zjly = null;
                            String qsrq = null;
                            String sjly = null;
                            String qsje = null;
                            ywlx = String.valueOf(allFormData.get("ywlx"));
                            if (ywlx.contains("消费")) {
                                ywlx = "积分（消费送红包）";
                            } else if (ywlx.contains("普通")) {
                                ywlx = "积分（普通）";
                            } else if (ywlx.contains("激励金")) {
                                ywlx = "奖励金";
                            }
                            // 下拉
                            zjly = String.valueOf(allFormData.get("zjly"));
                            qsrq = String.valueOf(allFormData.get("qsrq"));
                            // 下拉
                            sjly = FormDataUtils.getItemValue(formId, "sjly", "手工导入");
                            qsje = String.valueOf(allFormData.get("qsje"));
                            log.info("清算金额：" + qsje);

                            if (StrUtil.isNotBlank(ywlx) && StrUtil.isNotBlank(zjly) && StrUtil.isNotBlank(qsrq) && StrUtil.isNotBlank(sjly) && StrUtil.isNotBlank(qsje)) {
                                getService(user).addOrUpdateInfo(formId, formmodeId, user, ywlx, zjly, qsrq, sjly, qsje);
                            } else {
                                log.error("字段为空,请检查数据!");
                            }
                        }
                    }else {
                        log.error("获取数据为空!");
                    }
                }
            }
        } catch (Exception e) {
            log.info("all error == >" +e.getMessage());
            result.put("errmsg", "自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }
}
